aboutsummaryrefslogtreecommitdiff
path: root/pages/blog/[post].tsx
blob: 9ebd3ad825eaa93b9ceb70833215b201c1d4c17b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import { readdirSync, readFileSync } from 'fs';
import micromark from 'micromark';
import { join } from 'path';

import { NavBar } from '../../components/navbar';
import { CenteredPage, PageTitle } from '../../components/page';
import { Vierkant } from '../../components/ui';

export default function Post(props: {
	post: string;
	content: string;
	tags: string;
}) {
	return <div>
		<NavBar />
		<CenteredPage width={802}>
			<PageTitle>{props.post.replace(/_/g, ' ')}</PageTitle>
			<Vierkant fullwidth>
				<div dangerouslySetInnerHTML={{ __html: props.content }}>
				</div>
			</Vierkant>
		</CenteredPage>
	</div>;
}

function parseTags(fileContent: string) {
	var fileAsArr = fileContent.split('\n');
	var lastLine = fileAsArr[fileAsArr.length - 1];
	if (!lastLine.startsWith(';tags:')) {
		return {
			tags: [],
			result: '',
		};
	}

	var tags = lastLine.replace(';tags:', '').trim().split(' ');

	fileAsArr.pop();
	var result = fileAsArr.join('\n').trim();

	return { tags, result };
}

export function getStaticProps(props: { params: { post: string; }; }) {
	var filename = join('news/', props.params.post + '.md');
	var filecontent = readFileSync(filename).toString().trim();

	var parsed = parseTags(filecontent);
	var content = micromark(parsed.result);

	return {
		props: {
			post: props.params.post,
			content,
			tags: parsed.tags,
		},
	};
}

export function getStaticPaths() {
	var files = readdirSync('news').filter(f => f.endsWith('.md'));

	return {
		paths: files.map((f) => {
			return {
				params: {
					post: f.substr(0, f.length - 3),
				},
			};
		}),
		fallback: false,
	};
}